﻿<%@ Control Language="C#" AutoEventWireup="true" Inherits="Portal.Data.Webparts.WebpartControl" %>
<%@ Import Namespace="Portal.Core.Helper" %>
<%@ Import Namespace="Portal.Data.Config" %>
<%@ Import Namespace="Portal.Data.Webparts" %>
<%if (Items.Items.Count > 0)
  { %>
<span class="tit_thugian">
    <a target="_blank" href="http://quaphe.net/"><%=Config.DisplayTitle %></a>
</span>
<div class="thu_gian">
    <ul class="masonry-grid">
        <asp:Repeater ID="Items" runat="server">
            <ItemTemplate>
                <li class="grid-item">
                    <a target="_blank" href="<%#Eval("Url") %>" title='<%#Eval("FullName") %>'>
                        <img src='<%#Eval("Image") %>' alt='<%#Eval("FullName") %>' width="<%#Eval("Width") %>" height="<%#Eval("Height") %>"/>
                    </a>
                </li>
            </ItemTemplate>
        </asp:Repeater>
    </ul>
</div><!--END thu_gian-->
<script src="/Themes/JS/masonry.pkgd.min.js"></script>
<script>
    $(document).ready(function () {
        var $container = $('.masonry-grid');
        $container.masonry({
            columnWidth: '.grid-item',
            itemSelector: '.grid-item',
        });
        //fixWhenScroll('.thu_gian');
    });
    function fixWhenScroll(itemSelector) {
		var item = $(itemSelector);
        var ih = item.height() + item.offset().top;
        var maxb = $('#rightzone').offset().top + $('#rightzone').height();
        var valid = (item.height() < $('#rightzone').height());
		
        $(window).scroll(function () {
			if(valid)
			{
				var sb = $(window).scrollTop() + $(window).height();			
				if(sb>=maxb){
					item.css({ position: "fixed", bottom: (sb-maxb)+"px" });
				}
				else if (sb>ih) {
					item.css({ position: "fixed", bottom: "-10px" });
				} else {
					item.css({ position: "relative", bottom: "" });
				}
			}
        });
    };
</script>
<% } %>
<script runat="server">    
    public override WebpartConfig NewConfig
    {
        get { return new GetMaxConfig(); }
    }

    public GetMaxConfig PartConfig
    {
        get { return (GetMaxConfig)Config; }
    }

    private const string UrlFormat = "http://quaphe.net/services/jsondata.aspx?max={0}";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var jsondata = Portal.Data.Crawler.Common.GetHtmlSources(string.Format(UrlFormat, PartConfig.Max));
            if (jsondata.Length > 0)
            {
                var list = JsonHelper.JsonDeserialize<List<Portal.Data.Entity.QuaPheJson>>(jsondata);
                Items.DataSource = list;
                Items.DataBind();
            }
        }
    }
    
</script>
